
module Tower{
    var hero: Laya.Sprite;
    /*
    * 初始化地图
    */
    export class MapBuilder{
        constructor(){
            Laya.init(416,416,Laya.WebGL);
            var res:Array<any> = [ 
                {url:"res/atlas/images.atlas",type:Laya.Loader.ATLAS},
                {url:"res/atlas/images.png",type:Laya.Loader.IMAGE},
                ];

          //设置progress Handler的第4个参数为true，根据加载文件个数获取加载进度
          Laya.loader.load(res,null,Laya.Handler.create(this,this.onProgress,null,false));
        }

       onProgress(pro:number){
            var floor = new Floor1();
            Laya.stage.addChild(floor)
            
            hero = new Hero();
            Laya.stage.addChild(hero)
            hero.x = 160;
            hero.y = 320;
            Laya.stage.on(Laya.Event.KEY_UP,this,this.onkeyup);  
            Laya.stage.on(Laya.Event.KEY_DOWN,this,this.onkeydown);  
            Laya.timer.frameLoop(15,this,this.loop);  
        }


        direction = -1;


        onkeyup(e){
            this.direction = -1;
        }  
    
        onkeydown(e){
            this.direction = e.keyCode;
        }  
    
        loop(){
            console.log(this.direction)
            if (this.direction != -1){
                switch (this.direction){
                    case 37://左
                    {
                        hero.x -= 32
                        break;
                    }
                    case 38://上
                    {
                        hero.y -= 32
                        break;
                    }
                    case 39://右
                    {
                        hero.x += 32
                        break;
                    }
                    case 40://下
                    {
                        hero.y += 32
                        break;
                    }
                }
            }
            // if(this.keyList != null &&this.keyList.length > 0){
            //     // this.direction = this.keyList[0]
            //     console.log(this.keyList)
            // }
        }
    }
    class Hero extends Laya.Sprite{

        img: Laya.Texture;

        constructor(){
            super();
            this.img = Laya.loader.getRes("images/player_1.jpg");
            this.graphics.drawTexture(this.img, 32, 32)
        }
    }

    class Floor1 extends ui.FloorUI{
        constructor(){
            super();
            this.hide.visible = false;
            this.floor.visible = true;
            this.body.visible = true;
        }
    }
}

